package com.strange.mdl.module.mdl.service.debatehall;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.SortingField;
import com.strange.mdl.framework.common.util.string.StrUtils;
import com.strange.mdl.module.mdl.dal.dataobject.debatehall.DebatehallUserDO;
import com.strange.mdl.module.mdl.dal.dataobject.debatehallcomment.DebateHallCommentUserDO;
import com.strange.mdl.module.mdl.dal.dataobject.debatehallcomment.DebatehallCommentDO;
import com.strange.mdl.module.mdl.dal.mysql.debatehall.DebatehallMapper;
import com.strange.mdl.module.mdl.dal.mysql.debatehallcomment.DebatehallCommentMapper;
import com.strange.mdl.module.mdl.service.debatehallcomment.DebatehallCommentService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.strange.mdl.module.mdl.controller.admin.debatehall.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.debatehall.DebatehallDO;
import com.strange.mdl.framework.common.util.object.BeanUtils;


import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;
import static com.strange.mdl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 辨经殿话题 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class DebatehallServiceImpl implements DebatehallService {

    @Resource
    private DebatehallMapper debatehallMapper;

    @Resource
    private DebatehallCommentMapper debatehallCommentMapper;

    @Resource
    private DebatehallCommentService debatehallCommentService;

    @Override
    public Long createDebatehall(DebatehallSaveReqVO createReqVO) {
        // 插入
        DebatehallDO debatehall = BeanUtils.toBean(createReqVO, DebatehallDO.class);
        debatehallMapper.insert(debatehall);
        // 返回
        return debatehall.getId();
    }

    @Override
    public void updateDebatehall(DebatehallSaveReqVO updateReqVO) {
        // 校验存在
        validateDebatehallExists(updateReqVO.getId());
        // 更新
        DebatehallDO updateObj = BeanUtils.toBean(updateReqVO, DebatehallDO.class);
        debatehallMapper.updateById(updateObj);
    }

    @Override
    public void deleteDebatehall(Long id) {
        // 校验存在
        validateDebatehallExists(id);
        // 删除
        debatehallMapper.deleteById(id);
    }

    @Override
    public void deleteDebatehallListByIds(List<Long> ids) {
        // 校验存在
        validateDebatehallExists(ids);
        // 删除
        debatehallMapper.deleteByIds(ids);
    }

    private void validateDebatehallExists(List<Long> ids) {
        List<DebatehallDO> list = debatehallMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEBATEHALL_NOT_EXISTS);
        }
    }

    private void validateDebatehallExists(Long id) {
        if (debatehallMapper.selectById(id) == null) {
            throw exception(DEBATEHALL_NOT_EXISTS);
        }
    }

    @Override
    public DebatehallDO getDebatehall(Long id) {
        return debatehallMapper.selectById(id);
    }


    @Override
    public PageResult<DebatehallDO> getDebatehallPage(DebatehallPageReqVO pageReqVO) {

        LambdaQueryWrapper<DebatehallDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageReqVO.getId() != null, DebatehallDO::getId, pageReqVO.getId());
        wrapper.like(StrUtil.isNotEmpty(pageReqVO.getKeyword()), DebatehallDO::getContent, pageReqVO.getKeyword());
//        List<SortingField> sortingFields = List.of(new SortingField().setField("id").setOrder("desc"));
        PageResult<DebatehallDO> debatehallDOPageResult = debatehallMapper.selectPage(pageReqVO, null, wrapper);

        Long loginUserId = getLoginUserId();
        List<DebatehallDO> list = debatehallDOPageResult.getList();

        if (loginUserId != null) {
            for (DebatehallDO debatehallDO : list) {
                DebatehallUserDO duserDO = debatehallMapper.selectEbatehallUsers(debatehallDO.getId(), loginUserId);
                if (duserDO != null) {
                    debatehallDO.setIsLike(duserDO.getIsLike());
                    debatehallDO.setIsCollect(duserDO.getIsCollect());
                    debatehallDO.setIsShare(duserDO.getIsShare());
                    debatehallDO.setIsMessage(duserDO.getIsMessage());
                }
            }
        }
        return debatehallDOPageResult;
    }


    @Override
    public DebatehallDO get(Long id) {
        Long userId = getLoginUserId();
        List<DebatehallDO> debateHallEntities = debatehallMapper.selectPageList(null, id);
        DebatehallDO debatehallDO = debateHallEntities.get(0);
        if (userId != null) {
            DebatehallUserDO duserDO = debatehallMapper.selectEbatehallUsers(debatehallDO.getId(), userId);
            if (duserDO != null) {
                debatehallDO.setIsLike(duserDO.getIsLike());
                debatehallDO.setIsCollect(duserDO.getIsCollect());
                debatehallDO.setIsShare(duserDO.getIsShare());
                debatehallDO.setIsMessage(duserDO.getIsMessage());
            }
        }
        // 相关评论以及当前用户是否点赞
        List<DebatehallCommentDO> debateHallCommentDTOS = debatehallCommentService.commentByDetail(debatehallDO.getId(), userId);
        debatehallDO.setComments(debateHallCommentDTOS);
        return debatehallDO;
    }

    @Override
    public void operatecomment(Long userId, Long commentId, Integer status) {
        DebateHallCommentUserDO entity = new DebateHallCommentUserDO();
        entity.setUserId(userId);
        entity.setCommentId(commentId);
        entity.setIsLike(status);
        debatehallCommentMapper.updateCommentUsers(entity);
    }

    @Override
    public DebatehallDO support(Long userId, Long debateId, Integer type) {
        DebatehallUserDO duserDO = debatehallMapper.selectEbatehallUsers(debateId, userId);
        if (duserDO == null) {
            duserDO = new DebatehallUserDO();
            duserDO.setDebateId(debateId);
            duserDO.setUserId(userId);
            duserDO.setSide(type);
            debatehallMapper.insertEbatehallUsers(duserDO);
        } else {
            duserDO.setDebateId(debateId);
            duserDO.setUserId(userId);
            duserDO.setSide(type);
            debatehallMapper.updateDebateHallUser(duserDO);
        }

        DebatehallDO debateHallEntity = debatehallMapper.getDebateHallUserStatus(debateId);
        return debateHallEntity;
    }

    @Override
    public DebatehallDO operate(Long userId, Long debateId, String tag, Integer status) {
        DebatehallUserDO duserDO = debatehallMapper.selectEbatehallUsers(debateId, userId);
        if (duserDO != null) {
            if ("isLike".equals(tag)) {
                duserDO.setIsLike(status);
            }
            if ("isCollect".equals(tag)) {
                duserDO.setIsCollect(status);
            }
            if ("isShare".equals(tag)) {
                duserDO.setIsShare(status);
            }
            if ("isMessage".equals(tag)) {
                duserDO.setIsMessage(status);
            }
            debatehallMapper.updateDebateHallUser(duserDO);
        } else {
            duserDO = new DebatehallUserDO();
            if ("isLike".equals(tag)) {
                duserDO.setIsLike(status);
            }
            if ("isCollect".equals(tag)) {
                duserDO.setIsCollect(status);
            }
            if ("isShare".equals(tag)) {
                duserDO.setIsShare(status);
            }
            if ("isMessage".equals(tag)) {
                duserDO.setIsMessage(status);
            }
            duserDO.setDebateId(debateId);
            duserDO.setUserId(userId);
            debatehallMapper.insertEbatehallUsers(duserDO);
        }

        DebatehallDO debatehallDO = debatehallMapper.getDebateHallUserStatus(debateId);
        debatehallDO.setIsLike(duserDO.getIsLike());
        debatehallDO.setIsCollect(duserDO.getIsCollect());
        debatehallDO.setIsMessage(duserDO.getIsMessage());
        debatehallDO.setIsShare(duserDO.getIsShare());
        return debatehallDO;
    }

}